function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
$(function () {
    evaluateToPage(0);
    $(window).scroll(function () {
        //$(document).scrollTop() 获取垂直滚动的距离
        //$(document).scrollLeft() 这是获取水平滚动条的距离
        if ($(document).scrollTop() <= 0) {
            evaluateToPage(0);
        }
        if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
            var page = $("#page").val();
            evaluateToPage(page);
        }
    });
});

function evaluateToPage(page) {
    var storyid = $("#storyid").val();
    var url = "/except/shopcontroller/storyevaluate.do";
    var token = $("#token").val();
    if (isempty(token))
        token = "null";
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            storyid: storyid,
            page: page,
            count: 10,
            json: jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                var datas = data.data;
                var evaluateinfo = '';
                $(datas).each(function () {
                    var reply = this.reply;
                    var imgurl;
                    if (this.ispraise)
                        imgurl = 'img/praise_red.png';
                    else
                        imgurl = 'img/praise.png';
                    evaluateinfo += '<div class="about_item_all">' +
                        '<div class="div_head">' +
                        '<img  src="' + this.headimg + '" onerror="imgerror(this, \'img/onerror.png\');"/>' +
                        '</div>' +
                        '<div class="div_right">' +
                        '<div class="name_and_priase">' +
                        '<span class="left_name">' + this.nickname + '</span>' +
                        '<div class="right_priase">' +
                        '<span id="praise' + this.evaluate_id + '">' + this.praisecount + '</span>' +
                        '<img class="img_priase" onclick="praiseevalute(this,\'' + this.evaluate_id + '\', \'' + this.ispraise + '\');" src="' + imgurl + '">' +
                        '</div>' +
                        '</div><br />' +
                        '<span class="item_comment_desc_details">' + this.evaluate_desc + '</span>' +
                        '<span class="item_time_details">' + this.addtime + '</span>' +
                        '<div class="recommnd_div_list">';
                    $(reply).each(function () {
                        evaluateinfo += '<div class="guest_service_bg">' +
                            '<span>摘记回复:</span>' +
                            '<span>' + this.reply_text + '</span>' +
                            '</div>';
                    });
                    evaluateinfo += '</div>' +
                        '</div>' +
                        '</div><hr style="height: 1px; width: 97%; float: right; background-color: #f4f4f4; border: none;"/>';
                });
                if (page == 0) {
                    $("#evaluate").empty().append(evaluateinfo);
                    $("#nullmore").hide();
                }
                else
                    $("#evaluate").append(evaluateinfo);
                $("#page").val(page * 1 + 1);
                remove_advertising();
            } else if (code == 20013) {
                $("#nullmore").show();
            } else {
                $("#hintbg").show()
                $("#hint_message").html(message);
            }
        }
    });
}


function sendEvaluate() {
    var evaluatetext = $("#evaluatetext").val();
    if (isempty(evaluatetext)) {
        $("#hintbg").show()
        $("#hint_message").html("请输入精选留言");
        return;
    }
    var url = "/except/shopcontroller/evaluate.action";
    var token = $("#token").val();
    if (isempty(token))
        token = "null";
    var jsonstr = "{token:" + token + "}";
    var storyid = $("#storyid").val();
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            storyid: storyid,
            text: evaluatetext,
            json: jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                $("#evaluatetext").val('');
                $("#hintbg").show()
                $("#hint_message").show().html("留言成功");
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else {
                $("#hintbg").show()
                $("#hint_message").html(message);
            }
        },
        error: function (data) {
            $("#hintbg").show()
            $("#hint_message").html("网络异常");
        }
    });
}

function praiseevalute(img, evaluate_id, ispraise) {
    if (ispraise == "true") {
        $("#hintbg").show()
        $("#hint_message").html("您已点赞，请勿重复操作");
    } else {
        var url = "/except/shopcontroller/praise.action";
        var token = $("#token").val();
        if (isempty(token))
            token = "null";
        var jsonstr = "{token:" + token + "}";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                evaluateid: evaluate_id,
                json: jsonstr
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                if (code == 20000) {
                    $("#hintbg").show()
                    $("#hint_message").html("点赞成功");
                    var praisecount = $("#praise" + evaluate_id).html();
                    $("#praise" + evaluate_id).html(praisecount * 1 + 1);
                    $(img).attr("src", "img/praise_red.png");
                } else if (code == 10001 || code == 10002) {
                    location.href = "/except/login.jsp";
                } else {
                    $("#hintbg").show()
                    $("#hint_message").html(message);
                }
            },
            error: function (data) {
                $("#hintbg").show()
                $("#hint_message").html("网络异常");
            }
        });
    }
}